package com.liyujie.networkrequest.utils;

import android.text.TextUtils;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.liyujie.networkrequest.common.Constants;
import com.liyujie.networkrequest.mobile.event.MessageEvent;
import com.liyujie.networkrequest.mobile.event.MessageType;
import com.liyujie.networkrequest.utils.serialport.OpenPortUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonUtils {

    /**
     * 正在获取反馈
     */
    public static void AcquireFeedbackData(){
        Constants.LockCnts = 1;
        /**
         * 反馈接受数据
         */
        ArrayList<Byte> tempList = new ArrayList<>();
        /**
         * 上传数据
         */
        HashMap<Integer,Long> UploadData = new HashMap<>();
        /**
         * 本地反馈数据
         */
        String mDefaultLatticeState = SPUtils.getInstance().getString("DefaultLatticeState");
        do {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    byte[] feedbackData = OpenPortUtils.gainFeedback(Constants.LockCnts);
                    LogUtils.d("---------" + GsonUtils.toJson(feedbackData));
                    if(null != feedbackData){
                        Constants.LockCnts += feedbackData[5];
                        int startIndex = 6;
                        int endIndex = 17;
                        byte[] subArray = Arrays.copyOfRange(feedbackData, startIndex, endIndex + 1);
                        for (byte data : subArray){
                            tempList.add(data);
                        }
                    }else {
                        /**
                         * 正在获取反馈
                         */
                        Constants.BeBeingFeedback = false;
                        /**
                         * 判断长度
                         */
                        if(tempList.size() > 0){
                            /**
                             * byte[]转HashMap
                             */
                            byte[] feedbackByteArray = new byte[tempList.size()];
                            for (int i = 0; i < tempList.size(); i++) {
                                feedbackByteArray[i] = tempList.get(i).byteValue();
                            }
                            /**
                             * 判断是否是第一次
                             */
                            if(TextUtils.isEmpty(mDefaultLatticeState)){
                                SPUtils.getInstance().put("DefaultLatticeState", ConvertUtils.bytes2String(feedbackByteArray));
                            }else {
                                byte[] byteArray = ConvertUtils.string2Bytes(mDefaultLatticeState);
                                for (int i = 0; i < byteArray.length; i++){
                                    if(byteArray[i] != feedbackByteArray[i]){
                                        UploadData.put((i + 1), TimeUtils.getNowMills());
                                    }
                                }
                                EventBus.getDefault().post(new MessageEvent(MessageType.SendFeedback).put(UploadData));
                            }
                        }
                    }
                }
            }).start();
            try {
                Thread.sleep(1000); // 延迟指定时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }while (Constants.BeBeingFeedback);
    }




    /**
     * 将 byte[] 转换为 16 进制字符串可以使用以下方法：
     * @param bytes
     * @return
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b).toUpperCase();
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }
}
